document.onkeydown=function(event){
    var e = event || window.event || arguments.callee.caller.arguments[0];
    if(e && e.keyCode==13){ // enter 键
        var word=document.getElementById("searchinput").value;
        if(word){
            if(word.match(/^\s+$/g)){
                console.log("没有填信息")
            }else{
                location.href="searchdetail.html?searchKey="+word;
            }
        }
    }
};
var upanddown=83;
$(window).scroll(function(){
    var chajuli=$("#page_wrapper").offset().top - $(window).scrollTop();
    //导航条隐藏显示
    var a2=chajuli;
    if(a2<upanddown){
        //console.log("向下滚动");
        upanddown=a2;
        //向下滚动
        if(chajuli<36){
            $(".fixed_header").css({"position":"fixed","top":"-44px"});
            $(".wrapper.home_feed").css({"margin-top":"44px"});
            $("#page_wrapper").css("margin-top","44px");
        }
    }
    else{
        //console.log("向上滚动");
        upanddown=a2;
        //向上滚动
        if(0<chajuli){
            $(".fixed_header").css({"position":"absolute","top":"0px"});
            $(".wrapper.home_feed").css({"margin-top":"0"});
            $("#page_wrapper").css("margin-top","83px");
        }
    }
});
var id;
if($.cookie("MYID")){id=$.cookie("MYID")};
var returnurl=encodeURIComponent("http://www.pulu.com/weixin/zonghenews.html"); //同意后跳转页面
//所有方法--------------
function allftn(){
     if($.cookie("newstype")){
         if($.cookie("newstype")==1){
             $("#newzhzx2").parent().attr("class","HeaderSwitcherItem is_active");
             $("#newhyxw2").parent().attr("class","HeaderSwitcherItem");
             $("#newxq2").parent().attr("class","HeaderSwitcherItem");
             $("#zhzx").css("display","block");
             $("#hyxw").css("display","none");
             $("#xq").css("display","none");
         }else if($.cookie("newstype")==2){
             $("#newzhzx2").parent().attr("class","HeaderSwitcherItem");
             $("#newhyxw2").parent().attr("class","HeaderSwitcherItem is_active");
             $("#newxq2").parent().attr("class","HeaderSwitcherItem");
             $("#zhzx").css("display","none");
             $("#hyxw").css("display","block");
             $("#xq").css("display","none");
         }else if($.cookie("newstype")==3){
             $("#newzhzx2").parent().attr("class","HeaderSwitcherItem");
             $("#newhyxw2").parent().attr("class","HeaderSwitcherItem");
             $("#newxq2").parent().attr("class","HeaderSwitcherItem is_active");
             $("#zhzx").css("display","none");
             $("#hyxw").css("display","none");
             $("#xq").css("display","block");
         }
     }else{
         $("#newzhzx2").parent().attr("class","HeaderSwitcherItem is_active");
         $("#newhyxw2").parent().attr("class","HeaderSwitcherItem");
         $("#newxq2").parent().attr("class","HeaderSwitcherItem");
         $("#zhzx").css("display","block");
         $("#hyxw").css("display","none");
         $("#xq").css("display","none");
     };
     if(!$.cookie("zhzx")){
            //综合资讯
            $.ajax({
                type:"get",
                datatype:"json",
                url:zonghenewsurl+190+"&userId="+id,//api/typeinfo/GetSecondTypeByParentId?parentId=
                success:function(data){
                    //cookie保存数据
                    var arr=[];
                    for(var i=0;i<data.Data.length;i++){
                        //<span class='tc_unread'>
                        arr.push(data.Data[i].id+data.Data[i].typename);
                        $("#listbox").append("<li class='Cell' id="+data.Data[i].id+"><div class='tc_image'><img src='hyimg/"+data.Data[i].id+".png' class='topic_photo_img'/></div><span class='tc_inner' id='zh"+data.Data[i].id+"'><span class='tc_text'>"+data.Data[i].typename+"</span></span></span><div class='tc_stroke'></div></li>")
                    }
                    var expireDate = new Date();
                    expireDate.setTime( expireDate.getTime() + ( 4*60*60 * 1000 ) );
                    arr=arr.join(",");
                    $.cookie('zhzx',arr,{
                        expires :expireDate
                    });
                }
            });
            //行业新闻
            $.ajax({
                type:"get",
                datatype:"json",
                url:zonghenewsurl+191+"&userId="+id,//api/typeinfo/GetSecondTypeByParentId?parentId=
                success:function(data){
                    //cookie保存数据
                    var arr=[];
                    for(var i=0;i<data.Data.length;i++){
                        arr.push(data.Data[i].id+data.Data[i].typename);
                        $("#hylistbox").append("<li class='Cell' id="+data.Data[i].id+"><div class='tc_image'><img src='hyimg/"+data.Data[i].id+".png' class='topic_photo_img'/></div><span class='tc_inner'id='zh"+data.Data[i].id+"'><span class='tc_text'>"+data.Data[i].typename+"</span></span><div class='tc_stroke'></div></li>")
                    }
                    var expireDate = new Date();
                    expireDate.setTime( expireDate.getTime() + ( 4*60*60 * 1000 ) );
                    arr=arr.join(",");
                    $.cookie('hyxw',arr,{
                        expires :expireDate
                    });
                }
            });
            //兴趣爱好
            $.ajax({
                type:"get",
                datatype:"json",
                url:zonghenewsurl+192+"&userId="+id,
                success:function(data){
                    //cookie保存数据
                    var arr=[];
                    for(var i=0;i<data.Data.length;i++){
                        arr.push(data.Data[i].id+data.Data[i].typename);
                        $("#xqlistbox").append("<li class='Cell' id="+data.Data[i].id+"><div class='tc_image'><img src='hyimg/"+data.Data[i].id+".png' class='topic_photo_img'/></div><span class='tc_inner'id='zh"+data.Data[i].id+"'><span class='tc_text'>"+data.Data[i].typename+"</span></span><div class='tc_stroke'></div></li>")
                    }
                    var expireDate = new Date();
                    expireDate.setTime( expireDate.getTime() + ( 4*60*60 * 1000 ) );
                    arr=arr.join(",");
                    $.cookie('xqah',arr,{
                        expires :expireDate
                    });
                }
            });
     }
     else{
            //综合资讯
            //var data1=$.cookie("zhzx").slice(0,-1);
            var data1=$.cookie("zhzx").split(",");
            for(var i=0;i<data1.length;i++){
                var dataid=data1[i].match(/[0-9]+/)[0];
                var dataname=data1[i].replace(/[0-9]+/,"");
                $("#listbox").append("<li class='Cell' id="+dataid+"><div class='tc_image'><img src='hyimg/"+dataid+".png' class='topic_photo_img'/></div><span class='tc_inner' id='zh"+dataid+"'><span class='tc_text'>"+dataname+"</span></span></span><div class='tc_stroke'></div></li>")
            }
            //行业新闻
            //var data2=$.cookie("hyxw").slice(0,-1);
            var data2=$.cookie("hyxw").split(",");
            for(var i=0;i<data2.length;i++){
                var dataid=data2[i].match(/[0-9]+/)[0];
                var dataname=data2[i].replace(/[0-9]+/,"");
                $("#hylistbox").append("<li class='Cell' id="+dataid+"><div class='tc_image'><img src='hyimg/"+dataid+".png' class='topic_photo_img'/></div><span class='tc_inner' id='zh"+dataid+"'><span class='tc_text'>"+dataname+"</span></span></span><div class='tc_stroke'></div></li>")
            }
            //兴趣爱好
            //var data3=$.cookie("xqah").slice(0,-1);
            var data3=$.cookie("xqah").split(",");
            for(var i=0;i<data3.length;i++){
                var dataid=data3[i].match(/[0-9]+/)[0];
                var dataname=data3[i].replace(/[0-9]+/,"");
                $("#xqlistbox").append("<li class='Cell' id="+dataid+"><div class='tc_image'><img src='hyimg/"+dataid+".png' class='topic_photo_img'/></div><span class='tc_inner' id='zh"+dataid+"'><span class='tc_text'>"+dataname+"</span></span></span><div class='tc_stroke'></div></li>")
            }
     };
     $("#page_wrapper").delegate("li.Cell","click",function(e){
            var eventnode= e.target;
            var tpid=0;
            var nameword="";
            if(eventnode.className=="tc_text"){
                tpid=eventnode.parentNode.parentNode.id;
                nameword=eventnode.innerHTML;
            }else if(eventnode.className=="tc_inner"){
                tpid=eventnode.parentNode.id;
                nameword=eventnode.firstChild.innerHTML;
            }
         $.cookie("currentIndex","");
         $.cookie("scrollTop","");
         $.cookie("newsid","");
         $.cookie("totop","");
            //直接页面跳转
         $.cookie("newstypeid",tpid);
         $.cookie("newstypename",nameword);
         if($("#zhzx").css("display")==="block"){
             location.href="mycountry.html?from=zonghe";
         }else if($("#hyxw").css("display")==="block"){
             location.href="mycountry.html?from=hangye";
         }else if($("#xq").css("display")==="block"){
             location.href="mycountry.html?from=xingqu";
         }
     });
    //页面跳转
    $("#newzhzx2").click(function(){
        $.cookie("newstype","1");
        $("#newzhzx2").parent().attr("class","HeaderSwitcherItem is_active");
        $("#newhyxw2").parent().attr("class","HeaderSwitcherItem");
        $("#newxq2").parent().attr("class","HeaderSwitcherItem");
        $("#zhzx").css("display","block");
        $("#hyxw").css("display","none");
        $("#xq").css("display","none");
        $("title").html("Pulu-资讯圈-综合资讯");
    });
    $("#newhyxw2").click(function(){
        $.cookie("newstype","2");
        $("#newzhzx2").parent().attr("class","HeaderSwitcherItem");
        $("#newhyxw2").parent().attr("class","HeaderSwitcherItem is_active");
        $("#newxq2").parent().attr("class","HeaderSwitcherItem");
        $("#zhzx").css("display","none");
        $("#hyxw").css("display","block");
        $("#xq").css("display","none");
        $("title").html("Pulu-资讯圈-行业兴趣");
    });
    $("#newxq2").click(function(){
        $.cookie("newstype","3");
        $("#newzhzx2").parent().attr("class","HeaderSwitcherItem");
        $("#newhyxw2").parent().attr("class","HeaderSwitcherItem");
        $("#newxq2").parent().attr("class","HeaderSwitcherItem is_active");
        $("#zhzx").css("display","none");
        $("#hyxw").css("display","none");
        $("#xq").css("display","block");
        $("title").html("Pulu-资讯圈-兴趣爱好");
    });
}
$(document).ready(function(){
    if(is_weixn()){
        if($.cookie("wxopenid")){
            //新闻分类
            if(/newstype=/.test(location.href)){
                var newstype=location.href.match(/newstype=[0-9]/)[0].match(/[0-9]/)[0];
                //alert(newstype);
                $.cookie("newstype",newstype);
            }
//======================================================
            //从登录页返回有openid没有id
            if($.cookie("MYID")){
                allftn();
            }
            else{
                location.href="login.html";
            }
//======================================================
        }
        else{
            var reg=new RegExp("code=[a-zA-Z0-9]*");
            if(window.location.href.match(reg)){
                var code=window.location.href.match(reg);
                $.ajax({
                    type:"GET",
                    url:"GetOpenId.ashx?"+code,
                    async: false,
                    success:function(data){
                        var dat=data;
                        $.cookie("wxod",dat);
                        //发请求获取用户的id
                        $.ajax({
                            type:"GET",
                            url:getwxopenidurl+dat,
                            success:function(d){
                                //如果没有用户id 则证明用户是第一次登陆 保存openid跳转至登录页面
                                if(!d.Data){
                                    $.cookie("wxopenid",dat);
                                    location.href="login.html";
                                }else if(d.Data&&d.Data.userId.length>0){
                                    $.cookie("wxopenid",d.Data.openId);
                                    $.cookie("MYID",d.Data.userId);
                                    id=d.Data.userId;
                                    //=========================================================================
                                    //新闻
                                    allftn();
                                    //=========================================================================
                                }
                            }
                        })
                    }
                })
            }
            else{
                //新闻分类
                if(/newstype=/.test(location.href)){
                    var newstype=location.href.match(/newstype=[0-9]/)[0].match(/[0-9]/)[0];
                    //alert(newstype);
                    $.cookie("newstype",newstype);
                }
                location.href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx658c34793ecfd9f9&redirect_uri="+returnurl+"&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
            }
        }
    }
    else{
        allftn();
    }
});
var allavawidth=parseInt(window.screen.availWidth);
var avawidth=parseInt(parseInt(window.screen.availWidth)/4);
console.log(avawidth);
//$(function() {
//    var startX, startY, endX, endY;
//    var showADID = 1;
//    document.getElementById("divADBox").addEventListener("touchstart", touchStart, false);
//    document.getElementById("divADBox").addEventListener("touchmove", touchMove, false);
//    document.getElementById("divADBox").addEventListener("touchend", touchEnd, false);
//    function touchStart(event) {
//        var touch = event.touches[0];
//        console.log(touch);
//        startY = touch.pageY;
//        startX = touch.pageX;
//    }
//    function touchMove(event) {
//        var touch = event.touches[0];
//        endX = touch.pageX;
//    }
//    function touchEnd(event) {
//        $(".showlist0"+showADID).hide();
//        console.log((startX - endX));
//        console.log(showADID);
//
//        if ((startX - endX) > 200) {
//            showADID++;
//            if (showADID >3) {
//                console.log(showADID);
//                showADID = 1;
//            }
//            $(".showlist0"+showADID).show();
//        }
//        else if((startX - endX) <-200){
//            showADID--;
//            if (showADID <1) {
//                console.log(showADID);
//                showADID = 3;
//            }
//            $(".showlist0"+showADID).show();
//        }
//        else{
//            $(".showlist0"+showADID).show();
//        }
//
//    }
//})